﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 12:49:29
// 负责人：lvsc
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	
	/// <summary>
	///订单费用 数据访问层
	/// </summary>	
	internal partial class NetWorkFeeConfig
	{		
    
        /// <summary>
    	/// 根据断言条件获取单个订单费用
    	/// </summary>
        public NetWorkFeeConfigInfo QuerySingle(Expression<Func<NetWorkFeeConfigInfo, bool>> predicate)
	    {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        
        /// <summary>
    	/// 根据断言条件获获取订单费用列表
    	/// </summary>
	    public IList<NetWorkFeeConfigInfo> QueryList(Expression<Func<NetWorkFeeConfigInfo, bool>> predicate)
	    {
	        return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
	    }

        /// <summary>
        /// 根据城市和业务获取价格
        /// </summary>
        /// <param name="citId"></param>
        /// <param name="bizTypeId"></param>
        /// <returns></returns>
        public List<NetWorkFeeConfigInfo> GetFeeConfigs(int citId, int bizTypeId)
        {
            List<NetWorkFeeConfigInfo> listInfo = new List<NetWorkFeeConfigInfo>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM NetWorkFeeConfig WITH(NOLOCK) WHERE CityID=@CityID AND BizTypeID=@BizTypeID AND IsActive=1 ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@CityID", DbType.Int32, citId);
            db.AddInParameter(dbc, "@BizTypeID", DbType.Int32, bizTypeId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    listInfo.Add(GetByReader(dr));
                }
            }
            return listInfo;
        }

        /// <summary>
        /// 批量取消价格配置
        /// </summary>
        /// <param name="feeConfigIds"></param>
        /// <returns></returns>
        public bool BatchCancelFeeConfigByIds(List<int> feeConfigIds,int cityId,int bizTypeId,int updateUserId)
        {
            if (feeConfigIds.Count==0)
            {
                return false;
            }
            string ids = string.Join(",", feeConfigIds.ToArray());
            string strSql = "update NetWorkFeeConfig set IsActive=0,UpdatedDate=getdate(),UpdatedUserID=@UpdatedUserID WHERE FeeConfigID not in("+ ids + ") and CityID=@CityID and BizTypeID=@BizTypeID AND IsActive=1";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            //设置参数
            db.AddInParameter(dbc, "@CityID", DbType.Int32, cityId);
            db.AddInParameter(dbc, "@BizTypeID", DbType.Int32, bizTypeId);
            db.AddInParameter(dbc, "@UpdatedUserID", DbType.Int32, updateUserId);
            return db.ExecuteNonQuery(dbc) > 0;
        }
    }
	
}